<template>
  <div class="resource-list">
    <div v-for="resource in resources" :key="resource.id" class="resource-item">
      <div class="resource-item-cover">
        <!-- <img :src="notice.cover.url" /> -->
        <img src="../assets/images/file-resource.png">
      </div>
      <div class="resource-item-main">
        <div class="resource-info" style="display: flex;justify-content: space-between;">
          <div class="resource-summary">
            <span style="font-size: large;">{{ resource.fileName }} </span>
          </div>
          <div>
            <button class="button is-small" style="background-color: #296218;color: white;border-color: #296218;" @click="download(resource)">
              <span>下载</span>
            </button>
          </div>
        </div>
        <div class="resource-meta">
          <div style="display: flex;justify-content: flex-start;">
            <div class="resource-meta-left">
              <span class="resource-meta-item">
                <span>类型：{{ resource.typesName }}</span>
              </span>
            </div>
            <div class="resource-meta-right">
              <span class="resource-meta-item">
                <span>大小：{{ resource.createdAt }}</span>
              </span>
            </div>
          </div>
          <div class="resource-meta-left">
            <span class="resource-meta-item">
              <span>上传时间：{{ resource.createdAt }}</span>
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
const props = defineProps({
  resources: {
    type: Array,
    default() {
      return [];
    },
    required: false,
  },
});

const download = (resource) => {
  let xhr = new XMLHttpRequest();
  xhr.open('GET', resource.filePath, true);
  xhr.responseType = 'blob';

  xhr.onload = function () {
    if (this.status === 200) {
      const blob = this.response;
      const link = document.createElement('a');
      link.href = window.URL.createObjectURL(blob);
      link.download = resource.fileName;
      link.click();
    }
  };
  xhr.send();
}

</script>

<style lang="scss" scoped>
.resource-list {
  margin: 0 !important;

  .resource-item {
    padding: 12px 12px;
    transition: background 0.5s;
    border-radius: 3px;
    background: var(--bg-color);
    line-height: 24px;

    &:not(:last-child) {
      margin-bottom: 10px;
    }

    display: flex;
    align-items: center;

    .resource-item-main {
      width: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .resource-info {
        // margin-bottom:5px;
        .resource-title {
          font-size: 25px;
          line-height: 30px;
          font-weight: 500;
          color: var(--text-color);
          overflow: hidden;
          text-overflow: ellipsis;
        }

        .resource-summary {
          font-size: 14px;
          color: var(--text-color2);
          overflow: hidden;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 3;
          text-align: justify;
          padding-top: 6px;
          word-break: break-all;
          text-overflow: ellipsis;
        }
      }

      .resource-meta {
        display: flex;
        justify-content: space-between;
        font-size: 13px;
        padding-top: 6px;

        .resource-meta-left {
          .resource-meta-item {
            padding: 0 6px 0 0;
            color: var(--text-color3);

            .resource-author {
              font-weight: bold;
              padding: 0 3px;
            }
          }
        }

        .resource-meta-right {
          @media screen and (max-width: 768px) {
            & {
              display: none;
            }
          }

          margin-left: 10px;

          .resource-tag {
            padding: 2px 8px;
            justify-content: center;
            align-items: center;
            border-radius: 12.5px;
            margin-right: 10px;
            background: var(--bg-color2);
            border: 1px solid var(--border-color2);
            color: var(--text-color3);
            font-size: 12px;

            &:hover {
              color: var(--text-link-color);
              background: var(--bg-color);
            }
          }
        }
      }
    }

    .resource-item-cover {
      display: flex;
      margin:0 15px 0 6px;
      img {
        min-width: 64px;
        min-height: 64px;
        width: 64px;
        height: 64px;
        object-fit: cover;

        // @media screen and (max-width: 768px) {
        //   & {
        //     min-width: 110px;
        //     min-height: 80px;
        //     width: 110px;
        //     height: 80px;
        //   }
        // }
      }
    }
  }
}
</style>
